import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QMessageBox
from view import login,login1,choose_model,exercise,title
import user_dao
import random

model = 1 #1代表练习模式，2代表测试模式
symbol = 1 #1为加 2为减 3为乘 4为除

class myLogin(QMainWindow,login.Ui_mainWindow):
    #构造方法(登录窗口)
    def __init__(self):
        super(myLogin,self).__init__()
        self.setupUi(self)
    def register(self):
        #监听内容(跳转注册窗口)
        self.hide()
        self.my = myRegister()
        self.my.show()
    def login(self):
        #访问数据库验证成功跳转
        # print(self.le_id.text() +" "+self.le_passwd.text())
        sql = 'SELECT * FROM user where id=%s and passwd=%s;'
        if user_dao.query(sql, (self.le_id.text(),self.le_passwd.text())):
            self.hide()
            self.my = myChoose_model()
            self.my.show()
        else:
            #弹窗提示
            msg_box = QMessageBox(QMessageBox.Warning, '警告', '账号或密码错误,请重新输入')
            msg_box.exec_()
            self.le_id.setText("")
            self.le_passwd.setText("")

class myRegister(QMainWindow,login1.Ui_MainWindow):
    # 构造方法(注册窗口)
    def __init__(self):
        super(myRegister, self).__init__()
        self.setupUi(self)
    def register(self):
        id = self.le_id.text()
        passwd = self.le_passwd.text()
        name = self.le_name.text()
        sex = self.le_sex.text()
        if id=="" or passwd=="" or name=="" or sex=="":
            # 弹窗提示
            msg_box = QMessageBox(QMessageBox.Warning, '警告', '信息不能为空')
            msg_box.exec_()
        else:
            #首先判断是否存在过该账户
            sql_query = "SELECT  * FROM user where id = %s;"
            if user_dao.query(sql_query,args=id):
                # 弹窗提示
                msg_box = QMessageBox(QMessageBox.Warning, '警告', '该账户已存在，请重新设置')
                msg_box.exec_()
            else:
                sql_insert = "INSERT INTO user VALUES(%s,%s,%s,%s);"
                if user_dao.insert(sql_insert, (id, passwd, name, sex)) > 0:
                    # 弹窗提示
                    QMessageBox().information(None, "提示", "恭喜你，注册成功", QMessageBox.Yes)
                    self.hide()
                    self.my = myLogin()
                    self.my.show()
                else:
                    # 弹窗提示
                    msg_box = QMessageBox(QMessageBox.warning, '警告', '注册失败，请检查信息')
                    msg_box.exec_()
    def return1(self):
        self.hide()
        self.my = myLogin()
        self.my.show()

class myChoose_model(QMainWindow,choose_model.Ui_MainWindow):

    # 构造方法(登录后跳转)
    def __init__(self):
        super(myChoose_model, self).__init__()
        self.setupUi(self)
    def enter(self):
        global model
        #练习模式
        if self.rb_text.isChecked():
            model = 1
        #考试模式
        elif self.rb_exam.isChecked():
            model = 2
        self.hide()
        self.my = myExercise()
        self.my.show()

class myExercise(QMainWindow,exercise.Ui_MainWindow):
    # 构造方法(加减乘除)
    def __init__(self):
        super(myExercise, self).__init__()
        self.setupUi(self)
        # 设置小标签（练习或考试）
        if model == 1:
            self.lb_tips.setText("练习模式")
        elif model == 2:
            self.lb_tips.setText("考试模式")
    def last(self):
        self.hide()
        self.my = myChoose_model()
        self.my.show()
    def next(self):
        global symbol
        if self.rb_jia.isChecked():
            symbol = 1
        elif self.rb_jian.isChecked():
            symbol = 2
        elif self.rb_chen.isChecked():
            symbol = 3
        elif self.rb_chu.isChecked():
            symbol = 4
        self.hide()
        self.my = myTitle()
        self.my.show()

class myTitle(QMainWindow,title.Ui_MainWindow):
    # 构造方法(题目)
    global symbol
    a=[]
    b=[]
    def __init__(self):
        super(myTitle, self).__init__()
        self.setupUi(self)
        for i in range(10):
            self.a.append(int(random.random()*100+1))
            self.b.append(int(random.random()*100+1))
        self.lb_num1.setText("1")
        self.lb_a.setText(str(a[0]))
        self.lb_b.setText(str(b[0]))
        if symbol == 1:
            self.lb_symbol1.setText("+")
        elif symbol == 2:
            self.lb_symbol1.setText("-")
        elif symbol == 3:
            self.lb_symbol1.setText("x")
        elif symbol == 4:
            self.lb_symbol1.setText("÷")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywin = myLogin()
    mywin.show()
    sys.exit(app.exec_())
